package com.xctech.yace.swing.tab;


import com.xctech.yace.main.DbCompare;
import com.xctech.yace.main.FileCompare;
import com.xctech.yace.swing.ConfigValue;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ComparatorSwing {

    public static String title = "O3脚本比对";

    public static String pathRes = null;

    public static JPanel genComparePanel(Component parent) {

        //对比的表
        JPanel panel = new JPanel();
        panel.setLayout(null);

        JLabel tableName = new JLabel("原表名:");
        tableName.setBounds(10, 20, 70, ConfigValue.height);
        panel.add(tableName);

        JLabel tableNew = new JLabel("新表名:");
        tableNew.setBounds(10, 50, 70, ConfigValue.height);
        panel.add(tableNew);

        JTextField tables = new JTextField(20);
        tables.setBounds(80, 20, 900, ConfigValue.height);
        tables.setFont(new Font("Courier", Font.BOLD, 13));
        panel.add(tables);

        JTextField tablesNew = new JTextField(20);
        tablesNew.setBounds(80, 50, 900, ConfigValue.height);
        tablesNew.setFont(new Font("Courier", Font.BOLD, 13));
        panel.add(tablesNew);

        JLabel label = new JLabel("存储路径:");
        label.setBounds(10, 80, 100, ConfigValue.height);
        panel.add(label);
        JButton fileStore = new JButton("选择文件");
        fileStore.setBounds(110, 80, ConfigValue.sfiledW, ConfigValue.height);
        JLabel filePath = new JLabel();
        filePath.setBounds(230, 80, 900, ConfigValue.height);
        fileStore.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int option = fileChooser.showOpenDialog(parent);
                if (option == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    pathRes = file.getAbsolutePath();
                    filePath.setText(pathRes);
                }
            }
        });
        panel.add(filePath);
        panel.add(fileStore);

        //数据库比较
        JLabel label1 = new JLabel("数据库比较:");
        label1.setBounds(10, 110, 100, ConfigValue.height);
        panel.add(label1);

        JLabel j1 = new JLabel("数据库地址:");
        j1.setBounds(10, 140, 70, ConfigValue.height);
        panel.add(j1);
        JTextField url = new JTextField(20);
        url.setBounds(80, 140, 200, ConfigValue.height);
        url.setFont(new Font("Courier", Font.BOLD, 13));
        panel.add(url);

        JLabel j2 = new JLabel("实例名:");
        j2.setBounds(285, 140, 70, ConfigValue.height);
        panel.add(j2);
        JTextField sid = new JTextField(20);
        sid.setBounds(355, 140, 200, ConfigValue.height);
        sid.setFont(new Font("Courier", Font.BOLD, 13));
        panel.add(sid);

        JLabel j3 = new JLabel("用户名:");
        j3.setBounds(560, 140, 50, ConfigValue.height);
        panel.add(j3);
        JTextField username = new JTextField(20);
        username.setBounds(610, 140, 200, ConfigValue.height);
        username.setFont(new Font("Courier", Font.BOLD, 13));
        panel.add(username);

        JLabel j4 = new JLabel("密码:");
        j4.setBounds(815, 140, 30, ConfigValue.height);
        panel.add(j4);
        JPasswordField passowrd = new JPasswordField(20);
        passowrd.setBounds(850, 140, 200, ConfigValue.height);
        passowrd.setFont(new Font("Courier", Font.BOLD, 13));
        panel.add(passowrd);


        JLabel j5 = new JLabel("数据库地址:");
        j5.setBounds(10, 180, 70, ConfigValue.height);
        panel.add(j5);
        JTextField newUrl = new JTextField(20);
        newUrl.setBounds(80, 180, 200, ConfigValue.height);
        newUrl.setFont(new Font("Courier", Font.BOLD, 13));
        panel.add(newUrl);

        JLabel j6 = new JLabel("实例名:");
        j6.setBounds(285, 180, 70, ConfigValue.height);
        panel.add(j6);
        JTextField newSid = new JTextField(20);
        newSid.setBounds(355, 180, 200, ConfigValue.height);
        newSid.setFont(new Font("Courier", Font.BOLD, 13));
//        newUrl.setText("jdbc:oracle:thin:@192.168.0.63:1521:orcl");
        panel.add(newSid);

        JLabel j7 = new JLabel("用户名:");
        j7.setBounds(560, 180, 50, ConfigValue.height);
        panel.add(j7);
        JTextField newUsername = new JTextField(20);
        newUsername.setBounds(610, 180, 200, ConfigValue.height);
        newUsername.setFont(new Font("Courier", Font.BOLD, 13));
        panel.add(newUsername);

        JLabel j8 = new JLabel("密码:");
        j8.setBounds(815, 180, 30, ConfigValue.height);
        panel.add(j8);
        JPasswordField newPassowrd = new JPasswordField(20);
        newPassowrd.setBounds(850, 180, 200, ConfigValue.height);
        newPassowrd.setFont(new Font("Courier", Font.BOLD, 13));
        panel.add(newPassowrd);

        JButton execButton = new JButton("执行");
        execButton.setBounds(10, 210, ConfigValue.sfiledW, ConfigValue.height);
        panel.add(execButton);


        execButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Properties params = new Properties();
                params.setProperty("origin.jdbc.connector.driver", "oracle.jdbc.OracleDriver");
                params.setProperty("origin.jdbc.connector.url", "jdbc:oracle:thin:@" + url.getText().trim() + ":" + sid.getText().trim());
                params.setProperty("origin.jdbc.connector.username", username.getText().trim());
                params.setProperty("origin.jdbc.connector.password", passowrd.getText().trim());
                params.setProperty("target.jdbc.connector.driver", "oracle.jdbc.OracleDriver");
                params.setProperty("target.jdbc.connector.url", "jdbc:oracle:thin:@" + newUrl.getText().trim() + ":" + newSid.getText().trim());
                params.setProperty("target.jdbc.connector.username", newUsername.getText().trim());
                params.setProperty("target.jdbc.connector.password", newPassowrd.getText().trim());
                try {
                    if (pathRes == null || (tables.getText() == null || tables.getText().equals(""))) {
                        if (pathRes == null) {
                            JOptionPane.showMessageDialog(null, "请选择存储路径", "FileStore", JOptionPane.WARNING_MESSAGE);
                        } else {
                            JOptionPane.showMessageDialog(null, "请选择比对表名", "TableName", JOptionPane.WARNING_MESSAGE);
                        }
                    } else {
                        String[] tableOld = tables.getText().split(",");
                        String[] tableNew = tablesNew.getText().split(",");
                        Map<String, String> tableMap = new HashMap<>();
                        for (int i = 0; i < tableOld.length; i++) {
                            tableMap.put(tableOld[i], tableNew[i]);
                        }
                        boolean boo = DbCompare.dbCompare(params, tableMap, pathRes);
                        if (boo) {
                            JOptionPane.showMessageDialog(null, "数据库比对结束", "DbCompare", JOptionPane.WARNING_MESSAGE);
                        }
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "数据库比对失败:" + ex, "DbCompare", JOptionPane.ERROR_MESSAGE);
                }
            }
        });


        //文件比较
        JLabel label2 = new JLabel("文件比较:");
        label2.setBounds(10, 240, 100, ConfigValue.height);
        panel.add(label2);
        JButton button = new JButton("选择文件");
        button.setBounds(10, 270, ConfigValue.sfiledW, ConfigValue.height);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                int option = fileChooser.showOpenDialog(parent);
                if (option == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    try {
                        if (pathRes == null || (tables.getText() == null || tables.getText().equals(""))) {
                            if (pathRes == null) {
                                JOptionPane.showMessageDialog(null, "请选择存储路径", "FileStore", JOptionPane.WARNING_MESSAGE);
                            } else {
                                JOptionPane.showMessageDialog(null, "请选择比对表名", "TableName", JOptionPane.WARNING_MESSAGE);
                            }
                        } else {
                            boolean boo = FileCompare.fileFilter(file, pathRes, tables.getText().split(","));
                            if (boo) {
                                JOptionPane.showMessageDialog(null, "文件比对结束", "FileCompare", JOptionPane.WARNING_MESSAGE);
                            }
                        }
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(null, "文件比对失败:" + ex, "FileCompare", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        panel.add(button);
        return panel;
    }


}
